package com.ruoyi.wetest.utils;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheStats;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author Allen
 * @description TODO
 * @Date 2024/6/7 15:39
 */
@Component
public class L1CacheManager {

    private  Cache<String, Object> cache = CacheBuilder.newBuilder()
            .expireAfterAccess(10, TimeUnit.MINUTES)
            .recordStats()
            .maximumSize(100)
            .build();

    public void stats() {
        CacheStats stats = cache.stats();
        System.out.println(stats.toString());
    }

    public <J> J get(String key) {
        return (J) cache.getIfPresent(key);
    }

    public <J> void set(String key, J object) {
        cache.put(key, object);
    }

    public void del(String key) {
        cache.invalidate(key);
    }

}
